﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace TowerClimber
{
    class PlatformLine
    {
        #region PlatformVariables
        Platform.PlatformType[] platformsType;
        int numberPlats;
        float platformSpeed;
        Platform[] platforms;
        Vector2 lastPosition;
        
        #endregion

        public void initPlatLine()
        {
            platformsType = new Platform.PlatformType[3] { Platform.PlatformType.CommonSmall, Platform.PlatformType.CommonMedium, Platform.PlatformType.CommonLarge };
        }

        public void generatePlatLine(int numPlats, Vector2 P1position, float averageDistance, int[] platType, float platSpeed, Random random, int level)
        {
            //TODO: Listar as Plataformas em uma Lista na Linha e refazer código de renovação
            numberPlats = numPlats;
            platforms = new Platform[numPlats];
            lastPosition = P1position;
            platformSpeed = platSpeed;
            for (int i = 0 ; i < numPlats; i++)
            {
                platforms[i] = new Platform();
                platforms[i].position = new Vector2(lastPosition.X, lastPosition.Y);
                platforms[i].initPlatform(random, level);
                lastPosition.X += averageDistance + 96;
            }
        }

        public void platLineLoadContent(ContentManager contentManager)
        {
            for (int i = 0; i < numberPlats; i++)
            {
                platforms[i].platTexture = contentManager.Load<Texture2D>(platforms[i].platformTextureAsset);
            }
        }

        public void updatePlatLine(Player player, Vector2 playerLastPosition, int platDistance, int numberLines, Random random, int level, ContentManager content)
        {
            for (int i = 0; i < numberPlats; i++)
            {
                platforms[i].position.X += platformSpeed;
                if (platforms[i].position.X > 900)
                {
                    platforms[i].position.X = -400;
                }
                if (platforms[i].position.X < -400)
                {
                    platforms[i].position.X = 900;
                }
                if (platforms[i].position.Y > ((platDistance * numberLines) - 200))
                {
                    platforms[i].initPlatform(random, level);
                    platforms[i].platTexture = content.Load<Texture2D>(platforms[i].platformTextureAsset);
                    platforms[i].position.Y = -200;
                }

                if (platforms[i].touchingPlayer == true)
                {
                    if (player.jumpingUp == false)
                    {
                        player.position.X += platformSpeed;
                        if (platforms[i].slippery == true)
                        {
                            if (player.facingLeft == true)
                            {
                                player.position.X -= 1;
                            }
                            else { player.position.X += 1; }
                        }
                    }
                }

            }
            
        }

        public void updateCollision(Player player, Vector2 playerLastPosition)
        {
            for (int i = 0; i < numberPlats; i++)
            {
                platforms[i].testCollision(player, playerLastPosition);

            }
        
        }

        public void drawPlatLine(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < numberPlats; i++)
            {
                platforms[i].platBoundaries = new Rectangle((int)platforms[i].position.X, (int)platforms[i].position.Y, platforms[i].platTexture.Width, platforms[i].platTexture.Height);
                spriteBatch.Draw(platforms[i].platTexture, platforms[i].platBoundaries, Color.White);
            }
        }

        public void scrollPlats(float scrollSpeed)
        {
            for (int i = 0; i < numberPlats; i++)
            {
                platforms[i].scrollPlatform(scrollSpeed);
            }
        }
        
    }
}
